package com.jiuyin.dianjing.jiguang;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.jiuyin.dianjing.ui.activity.main.MainActivity;
import com.jiuyin.dianjing.util.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_MESSAGE;
import static com.jiuyin.dianjing.constant.AppConstant.MSG_EXTRA_COMPETE;

public class PushMessageReceiver extends JPushMessageReceiver {

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
//        super.onMessage(context, customMessage);
        LogUtil.log("[onMessage] " + customMessage);
        processCustomMessage(context, customMessage);
    }

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage message) {
//        super.onNotifyMessageOpened(context, message);
        LogUtil.log("[onNotifyMessageOpened] " + message.notificationExtras);
        String extra = message.notificationExtras;
        if (!TextUtils.isEmpty(extra)) {
            Intent i = new Intent(context, MainActivity.class);
            i.putExtra(KEY_MESSAGE, "1");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
//                JSONObject jsonObjectData = new JSONObject(extra);
//                LogUtil.log("[onNotifyMessageOpened] " + "bbbb");
//                if (jsonObjectData.has(MSG_EXTRA_COMPETE)) {
//                    String info = jsonObjectData.getString(MSG_EXTRA_COMPETE);
//                    LogUtil.log("[onNotifyMessageOpened] " + "aaaaa");
//                    JSONObject jsonObject = new JSONObject(info);
//                    Intent intent = new Intent(context, MatchInfoDetailActivity.class);
//                    intent.putExtra(KEY_COMPET_ID, jsonObject.getString("competitionId"));
//                    intent.putExtra(KEY_NAME, jsonObject.getString("title"));
//                    intent.putExtra(KEY_IS_CREATOR, jsonObject.getString("isCreator"));
//                    context.startActivity(intent);
//                }
        } else {
            try {
                //打开自定义的Activity
                Intent i = new Intent(context, MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(JPushInterface.EXTRA_NOTIFICATION_TITLE, message.notificationTitle);
                bundle.putString(JPushInterface.EXTRA_ALERT, message.notificationContent);
                i.putExtras(bundle);
                //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);
            } catch (Throwable throwable) {

            }
        }
    }

    @Override
    public void onMultiActionClicked(Context context, Intent intent) {
//        super.onMultiActionClicked(context, intent);
        LogUtil.log("[onMultiActionClicked] 用户点击了通知栏按钮");
        String nActionExtra = intent.getExtras().getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA);

        //开发者根据不同 Action 携带的 extra 字段来分配不同的动作。
        if (nActionExtra == null) {
            LogUtil.log("ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null");
            return;
        }
        if (nActionExtra.equals("my_extra1")) {
            LogUtil.log("[onMultiActionClicked] 用户点击通知栏按钮一");
        } else if (nActionExtra.equals("my_extra2")) {
            LogUtil.log("[onMultiActionClicked] 用户点击通知栏按钮二");
        } else if (nActionExtra.equals("my_extra3")) {
            LogUtil.log("[onMultiActionClicked] 用户点击通知栏按钮三");
        } else {
            LogUtil.log("[onMultiActionClicked] 用户点击通知栏按钮未定义");
        }
    }

    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage message) {
//        super.onNotifyMessageArrived(context, message);
        LogUtil.log("[onNotifyMessageArrived] " + message);
    }

    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage message) {
//        super.onNotifyMessageDismiss(context, message);
        LogUtil.log("[onNotifyMessageDismiss] " + message);
    }

    @Override
    public void onRegister(Context context, String registrationId) {
//        super.onRegister(context, registrationId);
        LogUtil.log("[onRegister] " + registrationId);
    }

    @Override
    public void onConnected(Context context, boolean isConnected) {
//        super.onConnected(context, isConnected);
        LogUtil.log("[onConnected] " + isConnected);
    }

    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {
//        super.onCommandResult(context, cmdMessage);
        LogUtil.log("[onCommandResult] " + cmdMessage);
    }

    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onTagOperatorResult(context, jPushMessage);
//        super.onTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context, jPushMessage);
//        super.onCheckTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context, jPushMessage);
//        super.onAliasOperatorResult(context, jPushMessage);
    }

    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context, jPushMessage);
//        super.onMobileNumberOperatorResult(context, jPushMessage);
    }

    //send msg to MainActivity
    private void processCustomMessage(Context context, CustomMessage customMessage) {
//        if (MainActivity.isForeground) {
//            String message = customMessage.message;
//            String extras = customMessage.extra;
//            Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
//            msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
//            if (!ExampleUtil.isEmpty(extras)) {
//                try {
//                    JSONObject extraJson = new JSONObject(extras);
//                    if (extraJson.length() > 0) {
//                        msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
//                    }
//                } catch (JSONException e) {
//
//                }
//
//            }
//            LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
//        }
        String extra = customMessage.extra;
        if (!TextUtils.isEmpty(extra)) {
            try {
                JSONObject jsonObjectData = new JSONObject(extra);
                if (jsonObjectData.has(MSG_EXTRA_COMPETE)) {

                } else if (jsonObjectData.has(MSG_EXTRA_COMPETE)) {
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        LogUtil.log("processCustomMessage extra = " + extra);
    }

}
